#include <stdio.h>

#define BASE_YEAR	1970

int is_leap(int year);
int days_month(int month, int year);

int main(void)
{
    int year, month;
    int sumdays = 0;
    int i;
    int first_day;
    int monthdays;

    do {
        printf("输入日期(month/year):");
        scanf("%d/%d", &month, &year);	
    } while(!(month >= 1 && month <= 12) || !(year >= BASE_YEAR));

    // 整年
    for (i = BASE_YEAR; i < year; i++) 
        sumdays += (365 + is_leap(i));

    // 1 ~ month
    for (i = 1; i < month; i++) 
        sumdays += days_month(i, year);

    // 1号
    sumdays += 1;
    printf("%d/%d/1距离计算机元年有%d天\n", year, month, sumdays);

    // 1号是星期几 为什么+3 因为我们想将星期几和余数对应起来，而计算机元年的1.1是星期4
    first_day = (sumdays + 3) % 7;
    printf("%d/%d/1是星期%d\n", year, month, first_day);

    // month月有多少天
    monthdays = days_month(month, year);

    // year.month.1是星期first_day, month月有monthdays天
    printf("      %d月%d\n", month, year);
    printf("日 一 二 三 四 五 六\n");
    for (i = 0; i < first_day; i++)	
        printf("   ");
    for (i = 1; i <= monthdays; i++) 
        printf("%2d%c", i, (i + first_day) % 7 == 0 ? '\n' : ' ');
    
    printf("\n");

    return 0;
}

int is_leap(int year)
{
    return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}

int days_month(int month, int year)
{
    int ret;

    if (1 == month || 3 == month || 5 == month || 7 == month || \
            8 == month || 10 == month || 12 == month)
        ret = 31;
    else if (4 == month || 6 == month || 9 == month || 11 == month)
        ret = 30;
    else if (2 == month) 
        ret = 28 + is_leap(year);
    else 
        ret = -1;

    return ret;
}


